package lei;

import java.util.LinkedList;
import java.util.Observable;

import javax.naming.OperationNotSupportedException;

import controller.delegates.BookshelfUIDelegate;
import model.EMedium;
import model.EMediumAttribute;
import model.EMediumPropertiesData;
import model.EMediumType;
import model.ILibraryFacade;
import model.IShelvesFacade;

/**
 * @author fmartins
 *
 * The bookshelf ui delegate default implementation. This is the class
 * you need to program in order to connect the UI events to your application
 * (the model).
 */
public class LEIBookshelfUIDelegate extends BookshelfUIDelegate {

	private IShelvesFacade shelvesHandler;
	private ILibraryFacade libraryHandler;

	/**
	 * 
	 * @param shelvesHandler
	 * @param libraryHandler
	 */
	public LEIBookshelfUIDelegate(IShelvesFacade shelvesHandler,
			ILibraryFacade libraryHandler) {

		this.shelvesHandler=shelvesHandler;
		this.libraryHandler=libraryHandler;
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #getShelfRentals(java.lang.String)
	 */
	@Override
	public Iterable<EMedium> getShelfRentals(String selectedShelf) {
		return new LinkedList<EMedium>();
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate#getRentals()
	 */
	@Override
	public Iterable<EMedium> getRentals() {
		return new LinkedList<EMedium>();
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate#getLibraryEMedia()
	 */
	@Override
	public Iterable<EMedium> getLibraryEMedia() {
		return new LinkedList<EMedium>();
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #addNormalShelf(java.lang.String)
	 */
	@Override //helder
	public boolean addNormalShelf(String shelfName) {
		return shelvesHandler.addNormalShelf(shelfName);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #removeShelf(java.lang.String)
	 */
	@Override //helder
	public void removeShelf(String shelfName) {
		shelvesHandler.removeShelf(shelfName);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #updateRental(model.EMedium, model.EMediumPropertiesData)
	 */
	@Override
	public void updateRental(EMedium document,
			EMediumPropertiesData documentProperties) {
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #removeEMediumShelf(java.lang.String, model.EMedium)
	 */
	@Override
	public void removeEMediumShelf(String shelfName, EMedium eMedium) 
			throws OperationNotSupportedException {
		shelvesHandler.removeRental(shelfName, eMedium);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate#getShelves()
	 */
	@Override
	public Iterable<String> getShelves() {
		return new LinkedList<String>();
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #addRentalEMedium(model.EMedium)
	 */
	@Override 
	public boolean addRentalEMedium(EMedium eMedium) {
		return shelvesHandler.addRental(eMedium);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #addEMediumShelf(java.lang.String, model.EMedium)
	 */
	@Override //Helder //Verificar
	public boolean addEMediumShelf(String shelfName, EMedium eMedium) 
			throws OperationNotSupportedException {		
		return shelvesHandler.addShelfRental(shelfName, eMedium);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #getEMediumTitle(model.EMedium)
	 */
	@Override //helder
	public String getEMediumTitle(EMedium d) {
		return d.getTitle();
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate#returnRental(model.EMedium)
	 */
	@Override //helder Verificar
	public void returnRental(EMedium eMedia) {
		shelvesHandler.returnRental(eMedia);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate#revokeLending(model.EMedium)
	 */
	@Override  //verificar
	public void revokeLending(EMedium eMedium) {
		eMedium.getEMediumProperties().getAttribute(EMediumAttribute.LICENSES);
		libraryHandler.rent(eMedium);
	}

	/*
	 * 
	 */
	@Override //helder  // TYPE nao string
	public boolean addEMediumLibrary(String type, 
			EMediumPropertiesData lendableProperties) {
		return libraryHandler.addLendable(EMediumType.valueOf(type), 
				lendableProperties);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #canBeViewed(model.EMedium)
	 */
	@Override //helder //verificar
	public boolean canBeViewed(EMedium eMedium) {
		return shelvesHandler.isRented(eMedium);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #isRent(model.EMedium)
	 */
	@Override //helder
	public boolean isRent(EMedium eMedium) {
		return libraryHandler.canBeRent(eMedium);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #shelfSelected(java.lang.String)
	 */
	@Override //helder
	public void shelfSelected(String selectedShelfName) {
		shelvesHandler.getShelfRentals(selectedShelfName);
	}

	/*
	 * (non-Javadoc)
	 * @see java.util.Observer
	 * #update(java.util.Observable, java.lang.Object)
	 */
	@Override  //helder
	public void update(Observable arg0, Object arg1) {
		arg0.notifyObservers(arg1);
	}

	/*
	 * (non-Javadoc)
	 * @see controller.delegates.BookshelfUIDelegate
	 * #addEMediumLibrary(java.lang.String, model.EMediumPropertiesData)
	 */
	@Override
	public boolean addEMediumLibrary(String type,
			EMediumPropertiesData properties) {
		// TODO Auto-generated method stub
		return false;
	}

}
